package satir.testcase.command;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.NoSuchElementException;

import org.junit.Test;

import satir.SatirGame;
import satir.command.Command;
import satir.command.EnterPlayerNumCmd;
import satir.framework.test.PrivateAccessor;
import satir.player.Player;
import junit.framework.TestCase;

public class EnterPlayerNumCmd_excute_2 extends TestCase {
	private EnterPlayerNumCmd command;
	private SatirGame mockSatirGame;

	/**
	 * Sets up the test fixture.
	 * 
	 * Called before every test case method.
	 */
	public void setUp() {
		mockSatirGame = new SatirGame();
		command = new EnterPlayerNumCmd(mockSatirGame);
	}

	/**
	 * Tears down the test fixture.
	 * 
	 * Called after every test case method.
	 */
	public void tearDown() {
	}

	/**
	 * Step number: 1 
	 * Goal: Set 2 players into ArrayList in SatirGame
	 */
	@Test
	public void testSet2PlayerInStatirGame() {
		int numberOfPlayer = 2;
		String input = String.valueOf(numberOfPlayer);
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		command.execute();
		try {
			ArrayList<Player> players = (ArrayList<Player>) PrivateAccessor
					.getPrivateField(mockSatirGame, "players");
			assertEquals(2, players.size());
		} catch (Exception e) {
			fail("Suberting JAVA Access Protection Failure.");
		}
	}

	/**
	 * Step number: 2 
	 * Goal: Set 5 players into ArrayList in SatirGame
	 */
	@Test
	public void testSet5PlayerInStatirGame() {
		int numberOfPlayer = 5;
		String input = String.valueOf(numberOfPlayer);
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		command.execute();
		try {
			ArrayList<Player> players = (ArrayList<Player>) PrivateAccessor
					.getPrivateField(mockSatirGame, "players");
			assertEquals(5, players.size());
		} catch (Exception e) {
			fail("Suberting JAVA Access Protection Failure.");
		}
	}

	/**
	 * Step number: 3 
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void testSet1PlayerInStatirGame() {
		String input = "1";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}

	/**
	 * Step number: 4 
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void testSet7PlayerInStatirGame() {
		String input = "7";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}

	/**
	 * Step number: 5 
	 * Goal:Print out an Error Message 
	 */
	@Test
	public void testSet123PlayerInStatirGame() {
		String input = "123";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}

	/**
	 * Step number: 6 
	 * Goal:Print out an Error Message 
	 */
	@Test
	public void testSet222PlayerInStatirGame() {
		String input = "122";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}

	/**
	 * Step number: 7 
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void testSetAPlayerInStatirGame() {
		String input = "A";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}

	/**
	 * Step number: 8 
	 * Goal: Print out an Error Message 
	 */
	@Test
	public void testSetSPlayerInStatirGame() {
		String input = "!";
		System.setIn(new ByteArrayInputStream(input.getBytes()));
		final ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
		System.setOut(new PrintStream(systemOut));
		try {
			command.execute();
		} catch (NoSuchElementException e) {
		}
		String actualResult = systemOut.toString();
		int startIndex = actualResult.indexOf('\n') + 1;
		actualResult = actualResult.substring(startIndex,
				actualResult.indexOf('\n', startIndex) - 1);
		assertEquals(actualResult, "Wrong input");
	}
}
